/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.synjones.pay.bank.wisedu.helper;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author liubinqiang
 * @date 2017-5-20 13:58:35
 * @version V1.0
 * @desc
 */
public class WisPayHelper {

    public static String createSignString(String[] values)
            throws UnsupportedEncodingException, NoSuchAlgorithmException {
        StringBuffer buf = new StringBuffer(512);
        for (int i = 0; i < values.length; i++) {
            String value = values[i];
            if (value == null) {
                continue;
            }
            buf.append(value);
        }
        byte[] bufTemp = buf.toString().getBytes("UTF-8");
        MessageDigest mdTemp = MessageDigest.getInstance("MD5");
        mdTemp.update(bufTemp);
        byte[] md5Result = mdTemp.digest();
        return byte2String(md5Result);
    }

    private static String byte2String(byte[] buf) {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            if ((buf[i] & 0xff) < 0x10) {
                result.append('0');
            }
            result.append((Integer.toHexString(buf[i] & 0xff)));
        }
        return result.toString();
    }

}
